package a07_二叉树;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * a13_二叉树的所有路径复习3
 * </p>
 *
 * @author flyduck
 * @since 2025/4/10
 */
public class a13_二叉树的所有路径复习3 {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> result = new ArrayList<>();
        LinkedList<Integer> path = new LinkedList<>();
        traversal(result, path, root);
        return result;
    }

    public static final String[] dirs = new String[]{"left","right"};

    private void traversal(List<String> result,
                           LinkedList<Integer> path,
                           TreeNode node){
        if(node != null){
            if(node.left == null && node.right == null){
                path.add(node.val);
                String collect = path.stream()
                        .map(item -> item.toString())
                        .collect(Collectors.joining("->"));
                result.add(collect);
                path.removeLast();
                return;
            }
        }else {
            return;
        }
        for (String dir : dirs) {
            path.add(node.val);
            if ("left".equals(dir)) {
                traversal(result, path, node.left);
            }else {
                traversal(result, path, node.right);
            }
            path.removeLast();
        }
    }
}
